package com.css.zfzx.sjcj.modules.hddcCjHistorypoints.repository.impl;

import com.css.zfzx.sjcj.modules.hddcCjHistorypoints.repository.HddcCjHistorypointsNativeRepository;
import com.css.zfzx.sjcj.modules.hddcCjHistorypoints.repository.entity.HddcCjHistorypointsEntity;
import com.css.zfzx.sjcj.modules.hddcCjHistorypoints.viewobjects.HddcCjHistorypointsQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * @author zhangping
 * @date 2020-12-23
 */
@Repository
public class HddcCjHistorypointsNativeRepositoryImpl implements HddcCjHistorypointsNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(HddcCjHistorypointsNativeRepositoryImpl.class);


    @Override
    public Page<HddcCjHistorypointsEntity> queryHddcCjHistorypointss(HddcCjHistorypointsQueryParams queryParams, int curPage, int pageSize,String sort,String order) {
        StringBuilder sql = new StringBuilder("select * from hddc_cj_historypoints ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getStartTime())) {
            whereSql.append(" and start_time >= :startTime");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getEndTime())) {
            whereSql.append(" and end_time <= :endTime");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getTaskId())) {
            whereSql.append(" and task_id = :taskId");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getRemark())) {
            whereSql.append(" and remark = :remark");
        }
        if (!PlatformObjectUtils.isEmpty(sort)) {
            whereSql.append(" order by "+sort+" "+order);
        }else {
            whereSql.append(" order by create_time desc");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HddcCjHistorypointsEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from hddc_cj_historypoints ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getStartTime())) {
            Date startTime = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                startTime = sf.parse(queryParams.getStartTime());
            } catch (Exception e) {
                log.error("时间格式转换错误");
            }
            query.setParameter("startTime", startTime);
            countQuery.setParameter("startTime", startTime);
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getEndTime())) {
            Date endTime = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                endTime = sf.parse(queryParams.getEndTime());
            } catch (Exception e) {
                log.error("时间格式转换错误");
            }
            query.setParameter("endTime", endTime);
            countQuery.setParameter("endTime", endTime);
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getTaskId())) {
            query.setParameter("taskId", queryParams.getTaskId());
            countQuery.setParameter("taskId", queryParams.getTaskId());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getRemark())) {
            query.setParameter("remark", queryParams.getRemark());
            countQuery.setParameter("remark", queryParams.getRemark());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<HddcCjHistorypointsEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<HddcCjHistorypointsEntity> hddcCjHistorypointsPage = new PageImpl<>(list, pageable, count.longValue());
        return hddcCjHistorypointsPage;
    }
}
